//
//  BCWalletPutPayPassController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/13.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCWalletPutPayPassController.h"


@interface BCWalletPutPayPassController ()

@property (nonatomic, strong) NSString *address;
@property (nonatomic, strong) NSString *amount;
@property (nonatomic, strong) NSString *curId;

@property (nonatomic, copy) BCWalletPutPayPassComplete completeBlock;

@end

@implementation BCWalletPutPayPassController

+ (void)showPutAddress:(NSString *)address amount:(NSString *)amount curId:(NSString *)curId fromController:(UIViewController *)fromController complete:(BCWalletPutPayPassComplete)complete {
    BControllerWindow *window = [[BControllerWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //
    BCWalletPutPayPassController *vc = [BCWalletPutPayPassController new];
    vc.fromController = fromController;
    vc.showWindow = window;
    vc.address = address;
    vc.amount = amount;
    vc.curId = curId;
    vc.completeBlock = complete;
    BCWindowNavigationController *navi = [[BCWindowNavigationController alloc] initWithRootViewController:vc];
    navi.navigationBar.hidden = true;
    navi.navigationBar.barStyle = [vc barStyleFromController:fromController];
    window.rootViewController = navi;
    //
    [window makeKeyAndVisible];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


- (void)passwordInputFinishAction {
    [self requestPutPayPassAction];
}

- (void)requestPutPayPassAction {
    [self startLoading];
    BCUserOperationCashRequest *request = [BCUserOperationCashRequest new];
    request.addr = self.address;
    request.cur_id = self.curId;
    request.amount = self.amount;
    request.pay_pass = self.passwordTF.text;
    //
    WEAK_SELF
    [request didRequestCompleted:^{
        [wself stopLoading];
    }];
    [request didRequestSuccessed:^(id result, id page) {
        BCUserOperationCashResponse *resp = result;
        [wself handleSuccess:resp];
    }];
    [request didRequestFailed:^(NSError *error) {
        wself.passwordTF.text = nil;
        if (error&&wself.contentView) {
            NSString *errMsg = error.domain;
            [QMUITips showError:errMsg inView:wself.contentView hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:errMsg]];
        }
    }];
    [request requestServer];
}



- (void)handleSuccess:(BCUserOperationCashResponse *)resp {
    if (self.completeBlock) {
        self.completeBlock(resp);
    }
    [self backButtonDidClickedAction];
}

@end
